import xlwings as xw

clazz_name = '查询类'

def creatHead(name, url):
    table = xw.Book('E:/download/库尔勒银行开放平台-网银接口(1).xlsx')  # 建立连接
    sht = table.sheets(clazz_name)
    name = sht.range(name).value
    url = sht.range(url).value
    name_list = name.split('-')
    str_up = '### ' + name_list[0] + '\n' \
                                     '本交易用于' + name_list[0] + '\n\n' \
                                                              '交易编码:' + name_list[1]

    print(url)
    str_down = '```\n' \
               '请求URL: ' + url + '\n' \
                                 '请求方法: POST \n' \
                                 '``` \n'
    str_head = str_up + '\n' + str_down
    print(str_head)
    return str_head


def creatReq(parm):
    table = xw.Book('E:/download/库尔勒银行开放平台-网银接口(1).xlsx')  # 建立连接
    sht = table.sheets(clazz_name)
    val = sht.range(parm).raw_value
    i = 0
    all_s = '**请求参数列表** \n''| 描述         | 请求参数      | 字段类型    | 是否必输 | 备注 |\n' \
            '| ------------ | ------------- | ----------- | -------- | ---- |\n' \
            '| 公共报文头 | **SYS_HEAD** |       |   |      |\n' \
            '| 公共报文头 | **APP_HEAD** |       |   |      |\n' \
            '|报文体 | **BODY** |        |  |      | \n'
    for v in val:
        name = delBody(v[0])
        sp = '  |    '
        v4 = v[4]
        if v[4] is None:
            v4 = ' '
        v4 =v4.replace('\n','')
        s = sp + v[2] + sp + name + sp + v[1] + sp + v[3] + sp + v4 + sp
        i += 1
        s += '\n'
        all_s += s
    print(all_s)
    print('总数:', i)
    return all_s
    pass


def createResp(parm):
    table = xw.Book('E:/download/库尔勒银行开放平台-网银接口(1).xlsx')  # 建立连接
    sht = table.sheets(clazz_name)
    val = sht.range(parm).raw_value
    print(val)
    i = 0
    all_s = '**响应参数列表**\n| 描述| 响应参数  | 字段类型 | 备注 | \n' \
            '|-------- | ------------ | -------- | --------------- |\n' \
            '| 公共报文头 | **SYS_HEAD** |          |      |\n' \
            '| 公共报文头 | **APP_HEAD** |          |      |\n' \
            '|报文体 | **BODY** |          |      | \n'

    for v in val:
        sp = '  |  '
        name = delBody(v[0])
        v4 = v[4]
        if v[4] is None:
            v4 = ' '
        v4 = v4.replace('\n', '')
        s = sp + v[2] + sp + name + sp + v[1] + sp + v4 + sp
        s += '\n'
        all_s += s
    print(all_s)
    print('总数:', i)
    return all_s
    pass


# 清楚body字符串
def delBody(str):
    return str.replace('BODY.', '')


def createPage(head1, head2, req, resp, file_name):
    head_page = creatHead(head1, head2)
    res_page = creatReq(req)
    resp_page = createResp(resp)
    page = head_page + '\n' + res_page + '\n' + resp_page
    with open('e://download/ibps/query/'+file_name+'.md', 'w', encoding='utf-8') as file:
        file.write(page)


w1 = 'C6:f9'
b1 = 'c13:g17'
#createPage('a1', 'd1', w1, b1)
# file_name = 'signUp'
# re = 'c6:g29'
# rp = 'c33:g34'
# createPage('a1','e1',re,rp,file_name)
#
# file_name = 'revoke'
# re = 'c41:g48'
# rp = 'c52:g52'
# createPage('a36','e36',re,rp,file_name)
#
# file_name = 'verify'
# re = 'c59:g73'
# rp = 'c77:g78'
# createPage('a54','e54',re,rp,file_name)
#
# file_name = 'auth'
# re = 'c85:g87'
# rp = 'c91:g91'
# createPage('a80','e80',re,rp,file_name)

# 支付类
# file_name = 'transfer'
# re = 'c6:g18'
# rp = 'c22:g22'
# createPage('a1','D1',re,rp,file_name)


# file_name = 'refund'
# re = 'c29:g39'
# rp = 'c43:g43'
# createPage('a24','D24',re,rp,file_name)

#
# file_name = 'debitTransfer'
# re = 'c50:g67'
# rp = 'c71:g71'
# createPage('a45','D45',re,rp,file_name)


# 查询类
# file_name = 'bankInfo'
# re = 'c6:g9'
# rp = 'c13:g17'
# createPage('a1','D1',re,rp,file_name)
# file_name = 'tradeResult'
# re = 'c24:g25'
# rp = 'c29:g37'
# createPage('a19','D19',re,rp,file_name)

# file_name = 'accBalance'
# re = 'c44:g46'
# rp = 'c50:g50'
# createPage('a39','D39',re,rp,file_name)

# file_name = 'tradeDetail'
# re = 'c57:g61'
# rp = 'c65:g65'
# createPage('a52','D52',re,rp,file_name)

# file_name = 'accBalanceReply'
# re = 'c72:g73'
# rp = 'c77:g97'
# createPage('a67','D67',re,rp,file_name)

# file_name = 'tradeDetailReply'
# re = 'c104:g105'
# rp = 'c109:g128'
# createPage('a99','D99',re,rp,file_name)

# file_name = 'contract'
# re = 'c135:g145'
# rp = 'c149:g183'
# createPage('a130','e130',re,rp,file_name)

# file_name = 'tranMessageInfo'
# re = 'c190:g201'
# rp = 'c205:g242'
# createPage('a185','e185',re,rp,file_name)

file_name = 'bindingRel'
re = 'c282:g282'
rp = 'c286:g296'
createPage('a277','e277',re,rp,file_name)